/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dio.com.gui;

import dio.com.model.Facade;
import dio.com.model.bean.Pendencia;
import dio.com.model.bean.Periodo;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;


/**
 *
 * @author Diogenes
 */
public class AnalisePendencias extends javax.swing.JFrame {
    
    private DefaultTableModel tmPendencias = new DefaultTableModel(null, new String[]{"Empresa","Imposto", "Valor", "Meta"});
    private List<Pendencia> pendencias = new ArrayList<Pendencia>();
    private Periodo p;
    
    /**
     * Creates new form AnalisePendencias
     */
    public AnalisePendencias(Periodo p) {
        this.p = p;
        initComponents();
        setLocationRelativeTo(null);
        listarPendencias(p);        
    }

    @Override
    public void toFront() {
        listarPendencias(p);
        super.toFront();
    }

   
    
    private void listarPendencias(Periodo p){
        pendencias = Facade.getInstancia().analisarPendencias(p);
        mostrarPendencias(pendencias);        
        lbTotal.setText(String.valueOf(pendencias.size()));
    }
    
    private void mostrarPendencias(List<Pendencia> pendencias){
        
        while (tbPendencia.getModel().getRowCount() > 0) {
            tmPendencias.removeRow(0);
//            tbPendencia.removeRowSelectionInterval(0, 0);
        }

        if (pendencias.isEmpty() || pendencias == null) {
            JOptionPane.showMessageDialog(this, "Nenhuma Pendência encontrada!","Informação",JOptionPane.INFORMATION_MESSAGE);
        } else {
            String[] campos = new String[]{null, null, null, null};
            for (int i = 0; i < pendencias.size(); i++) {
                tmPendencias.addRow(campos);
                tbPendencia.getModel().setValueAt(pendencias.get(i).getEmpresa().getNome(), i, 0);
                tbPendencia.getModel().setValueAt(pendencias.get(i).getImposto().getNome(), i, 1);
                tbPendencia.getModel().setValueAt(pendencias.get(i).getMovimento().getValor(), i, 2);
                tbPendencia.getModel().setValueAt(pendencias.get(i).getMeta().getValorMeta(), i, 3);

            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tbPendencia = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        lbTotal = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Análise de Pendências");

        tbPendencia.setModel(tmPendencias);
        jScrollPane1.setViewportView(tbPendencia);

        jLabel1.setText("Total de pendências:");

        lbTotal.setText("...");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lbTotal))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(34, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lbTotal))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbTotal;
    private javax.swing.JTable tbPendencia;
    // End of variables declaration//GEN-END:variables
}
